""" Builder of optimizers. """

import inspect

import mindspore as ms

from utils.class_factory import ClassFactory, ModuleType


def build_optimizer(cfg, default_args=None):
    opt = ClassFactory.get_instance_from_cfg(
        cfg, ModuleType.OPTIMIZER, default_args)
    return opt

def register_optimizers():
    """Register optimizers from nn.Optimizer in engine. """
    for module_name in dir(ms.nn):
        if not module_name.startswith('__'):
            opt = getattr(ms.nn, module_name)
            if inspect.isclass(opt) and issubclass(opt, ms.nn.Optimizer):
                ClassFactory.register_cls(opt, ModuleType.OPTIMIZER)
